package com.emep.changzhi.analyse.http.equipment;

import com.emep.changzhi.analyse.entity.equipment.MoitorTypeInfo;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.equipment.MoitorTypeInfoService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @version V1.0
 * @Description: 监测站点控制器
 * @date 2018/2/28  14:22
 */
@RestController
@RequestMapping("moitortype")
@Api(value="监测站点列表controller",tags={"监测站点列表操作接口"})
public class MoitorTypeInfoController {

    @Autowired
    private MoitorTypeInfoService moitorSiteInfoService;

    /**
     * 新增监测设备
     * @return
     * */
    @ApiOperation(value = "添加监测站点列表类型" , notes = "监测站点列表类型")
    @ApiResponses({
            @ApiResponse(code = 1,message = "操作成功"),
            @ApiResponse(code = 0,message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303,message = "权限不足")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "monitorTypeName", value = "监测类型名字", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = true, dataType = "String",paramType="query"),
    })
    @PostMapping(value = "add")
    public ResultBean<MoitorTypeInfo> add(@RequestParam Map<String , Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "monitorTypeName","areaCode");
        if(result.getCode() == 1){
            return result.boolAdd(moitorSiteInfoService.saveMoitorSiteInfo(payload)) ;
        }
        return  result;
    }


    /**
     * 查询所有监测类型列表
     * @return
     * */
    @ApiOperation(value="获取站点列表类型", notes="无参数获取所有站点列表类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    protected ResultBean<List<MoitorTypeInfo>> findAll(){
        return new ResultBean<List<MoitorTypeInfo>>(moitorSiteInfoService.getMoitorSiteInfoList());
    }

    /**
     * 不带条件的分页查询
     * */
    @ApiOperation(value="分页查询站点列表类型", notes="分页无参数查询站点列表类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping(" paging")
    public ResultBean<Page<MoitorTypeInfo>> showUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<MoitorTypeInfo> pageEmissionSourceInfo = moitorSiteInfoService.findMoitorSiteInfoNoCriteria(Integer.parseInt(payload.get("pageIndex").toString()),Integer.parseInt(payload.get("pageSize").toString()));
            return result.ok(pageEmissionSourceInfo);
        }
        return result;
    }


    /**
     * 带条件的分页查询
     * */
    @ApiOperation(value="分页带参数查询站点列表类型", notes="分页带参数查询站点列表类型")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = false, dataType = "String",paramType="query"),

    })
    @GetMapping(" paging/query")
    public ResultBean PageUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            MoitorTypeInfo moitorSiteInfo = new MoitorTypeInfo();
            moitorSiteInfo.setAreaCode(payload.containsKey("areaCode") ?payload.get("areaCode") != null ? payload.get("areaCode").toString():"":"" );
            Page<MoitorTypeInfo> pageEmissionSourceInfo = moitorSiteInfoService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),moitorSiteInfo);
            return result.ok(pageEmissionSourceInfo);
        }
        return result;
    }

    /**
     * 根据id查询监测设备
     * */
    @ApiOperation(value="根据id 查询站点列表类型", notes="根据监测设备的id来获取站点列表类型的详细信息")
    @ApiImplicitParam(name = "id", value = "监测设备ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "info")
    public ResultBean<MoitorTypeInfo> queryEquipmentById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS) {
            return result.ok(moitorSiteInfoService.findEmissionSourceInfoById(payload.get("id").toString())) ;

        }
        return result;
    }


    /**
     * 根据id删除监测设备
     * */
    @ApiOperation(value="删除站点列表", notes="根据站点id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "监测设备ID", required = true, dataType = "String")
    @DeleteMapping(value = "/delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            return result.bool(moitorSiteInfoService.delete(payload.get("id").toString())) ;
        }
        return result;
    }



}
